<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="核验补录" width="550px" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { addCheckFormSchema } from '../data/data';
import { addcheckRecord } from '../data/api';

// 声明Emits
const emit = defineEmits(['success', 'register']);
const isUpdate = ref(false);

const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: addCheckFormSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  let record = data.record;
  if (record) {
    let newData = { userGoodsId: record.userGoodsId, vipCardNo: record.vipCardNo };
    setFieldsValue({
      ...newData,
    });
  }
});

async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    if (!unref(isUpdate)) {
      await addcheckRecord(values);
    }
    closeDrawer();
    emit('success', false);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
